package com.example.edianzu.somur.utils.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;

import java.util.ArrayList;

/**
 * author：风不会停息 on 2017/8/3 13:31
 * mailbox：yh131412hys@163.com
 * project：AdvancedUI
 * tel：13295355900
 * describe：
 */

public class WrapRecyclerView extends RecyclerView {

    ArrayList<View> mHeaderViewInfos = new ArrayList<>();
    ArrayList<View> mFooterViewInfos = new ArrayList<>();
    private Adapter mAdapter;

    public WrapRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public void addHeaderView(View v) {

        mHeaderViewInfos.add(v);

        if (mAdapter != null) {
            if (!(mAdapter instanceof HeaderAndFooterRecyclerAdapter)) {
                mAdapter = new HeaderAndFooterRecyclerAdapter(mHeaderViewInfos, mFooterViewInfos, mAdapter);
            }
        }
    }

    public void addFooterView(View v) {

        mFooterViewInfos.add(v);

        // Wrap the adapter if it wasn't already wrapped.
        if (mAdapter != null) {
            if (!(mAdapter instanceof HeaderAndFooterRecyclerAdapter)) {
                mAdapter = new HeaderAndFooterRecyclerAdapter(mHeaderViewInfos, mFooterViewInfos, mAdapter);
            }
        }
    }

    @Override
    public void setAdapter(Adapter adapter) {

        if (mHeaderViewInfos.size() > 0|| mFooterViewInfos.size() > 0) {
            mAdapter = new HeaderAndFooterRecyclerAdapter(mHeaderViewInfos, mFooterViewInfos, adapter);
        } else {
            mAdapter = adapter;
        }

        super.setAdapter(mAdapter);
    }
}
